package com.example.forum2.Service.impl;

import com.example.forum2.Service.IMessageService;
import com.example.forum2.Service.IUserService;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.dao.MessageMapper;
import com.example.forum2.exception.ApplicationExeception;
import com.example.forum2.model.Message;
import com.example.forum2.model.User;
import com.example.forum2.utils.StringUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageImpy implements IMessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private IUserService userService;
    @Override
    public void create(Message message) {
        if(message==null||message.getPostUserId()==null||message.getReceiveUserId()==null||
                StringUtil.isEmpty(message.getContent())){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user =userService.selectById(message.getReceiveUserId());
        if(user==null||user.getDeleteState()==1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        int row = messageMapper.insertSelective(message);
        if(row!=1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(receiveUserId==null||receiveUserId==0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if(count==null){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId==null||receiveUserId<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if(id==null||id<=0||state<0||state>2){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date = new Date();
        updateMessage.setUpdateTime(date);
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row!=1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Message selectById(Long id) {
        if(id==null||id<0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if(repliedId==null||repliedId<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(existMessage==null||existMessage.getDeleteState()==1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        Message updateMessage = new Message();
        updateMessage.setId(repliedId);
        updateMessage.setState((byte) 2);

        updateStateById(repliedId,(byte) 2);
        create(message);
    }
}
